package com.softeem.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.softeem.crm.enums.DevResult;
import com.softeem.crm.enums.StateStatus;
import com.softeem.crm.mapper.UserMapper;
import com.softeem.crm.pojo.SaleChance;
import com.softeem.crm.pojo.User;
import com.softeem.crm.service.SaleChanceService;
import com.softeem.crm.mapper.SaleChanceMapper;
import com.softeem.crm.utils.AssertUtil;
import com.softeem.crm.utils.PhoneUtil;
import com.softeem.crm.vo.SaleChanceQuery;
import com.softeem.crm.vo.SaleChanceVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
* @author Administrator
* @description 针对表【t_sale_chance】的数据库操作Service实现
* @createDate 2022-12-27 14:23:06
*/
@Service
public class SaleChanceServiceImpl extends ServiceImpl<SaleChanceMapper, SaleChance>
    implements SaleChanceService{

    @Resource
    private UserMapper userMapper ;
    @Override
    public Map<String, Object> queryByParamsForTable(SaleChanceQuery saleChanceQuery) {
        Page<SaleChance> pageParam = new Page<>(saleChanceQuery.getPage(), saleChanceQuery.getLimit());

        LambdaQueryWrapper<SaleChance> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.like(StringUtils.isNotBlank(saleChanceQuery.getCustomerName()),SaleChance::getCustomerName,saleChanceQuery.getCustomerName())
                .eq(StringUtils.isNotBlank(saleChanceQuery.getCreateMan()),SaleChance::getCreateMan,saleChanceQuery.getCreateMan())
                .eq(StringUtils.isNotBlank(saleChanceQuery.getState()),SaleChance::getState,saleChanceQuery.getState())
                .eq(StringUtils.isNotBlank(saleChanceQuery.getAggsinMan()),SaleChance::getAssignMan , saleChanceQuery.getAggsinMan())
                .eq(saleChanceQuery.getDevResult()!=null, SaleChance::getDevResult , saleChanceQuery.getDevResult())


                .orderByDesc(SaleChance::getId);

        Page<SaleChance> saleChancePage= this.baseMapper.selectPage(pageParam, lambdaQueryWrapper);

        List<SaleChance> saleChanceList = saleChancePage.getRecords();

        ArrayList<SaleChanceVo> chanceVoArrayList = new ArrayList<>();
        //我现在要将SaleChance里面的数据都转存到SaleChanceVo
        for (SaleChance saleChance : saleChanceList) {
            SaleChanceVo saleChanceVo = new SaleChanceVo();
            //就是将原来的bean对象中的数据复制到vo对象中
            BeanUtils.copyProperties(saleChance , saleChanceVo);
            User user = userMapper.selectById(saleChanceVo.getAssignMan());
            if(user != null){
                saleChanceVo.setUname(user.getUserName());
            }
            chanceVoArrayList.add(saleChanceVo);
        }

        Map<String,Object> result = new HashMap<String,Object>();
        result.put("count",saleChancePage.getTotal());
        result.put("data",chanceVoArrayList);
        result.put("code",0);
        result.put("msg","");
        return result;
    }

    @Transactional
    @Override
    public void saveSaleChance(SaleChance saleChance) {
        checkParams(saleChance.getCustomerName(),saleChance.getLinkMan(),saleChance.getLinkPhone());
        saleChance.setState(StateStatus.UNSTATE.getType());
        saleChance.setDevResult(DevResult.UNDEV.getStatus());
        if( StringUtils.isNotBlank(saleChance.getAssignMan())){
            saleChance.setState(StateStatus.STATED.getType());
            saleChance.setDevResult(DevResult.DEVING.getStatus());
            saleChance.setAssignTime(new Date());
        }
        saleChance.setIsValid(0);
        AssertUtil.isTrue(!this.save(saleChance),"机会数据添加失败!");
    }

    @Transactional
    public void updateSaleChance(SaleChance saleChance){
        AssertUtil.isTrue( null ==saleChance.getId(),"待更新记录不存在!");
        //通过机会数据的主键id查询出机会数据 赋值给temp对象
        SaleChance temp = this.baseMapper.selectById(saleChance.getId());
        AssertUtil.isTrue( null ==temp,"待更新记录不存在!");
        checkParams(saleChance.getCustomerName(), saleChance.getLinkMan(), saleChance.getLinkPhone());
        //数据库里面的assign_man要为空或为空串  (并且)  用户提交上来的assign_man不为空或不为空串
        if(StringUtils.isBlank(temp.getAssignMan()) && StringUtils.isNotBlank(saleChance.getAssignMan())){
            saleChance.setState(StateStatus.STATED.getType());   //已分配
            saleChance.setAssignTime(new Date());
            saleChance.setDevResult(DevResult.DEVING.getStatus());//已开发
            //数据库里面的assign_man要不为空或不为空串  (并且)  用户提交上来的assign_man为空或为空串
        }else if(StringUtils.isNotBlank(temp.getAssignMan()) && StringUtils.isBlank(saleChance.getAssignMan())){
            saleChance.setAssignMan("");
            saleChance.setState(StateStatus.UNSTATE.getType()); //未分配
            saleChance.setAssignTime(null);
            saleChance.setDevResult(DevResult.UNDEV.getStatus());//未开发
        }else if (StringUtils.isNotBlank(temp.getAssignMan()) && StringUtils.isNotBlank(saleChance.getAssignMan())) {
            if (!temp.getAssignMan().equals(saleChance.getAssignMan())) {
                saleChance.setAssignTime(new Date());
            }
        }

        int update = this.baseMapper.update(saleChance,
                Wrappers.<SaleChance>lambdaUpdate().
                set(SaleChance::getAssignTime, saleChance.getAssignTime())
                .eq(SaleChance::getId,saleChance.getId())
        );
        AssertUtil.isTrue(update < 1,"机会数据更新失败!");
    }

    /**
     * 批量删除
     * @param ids
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteSaleChancesByIds(Integer[] ids){
        AssertUtil.isTrue(null == ids || ids.length==0,"请选择待删除的机会数据!");
        AssertUtil.isTrue(this.baseMapper.deleteBatchIds(Arrays.asList(ids))<ids.length,"机会数据删除失败!");
    }

    @Transactional
    public void updateSaleChanceDevResult(Integer id, Integer devResult) {
        AssertUtil.isTrue( null ==id,"待更新记录不存在!");
        SaleChance temp =this.getById(id);
        AssertUtil.isTrue( null ==temp,"待更新记录不存在!");
        temp.setDevResult(devResult);

        temp.setUpdateDate(null);

        AssertUtil.isTrue(!this.updateById(temp),"机会数据更新失败!");
    }
    /*
      表单基本参数校验
    */
    private void checkParams(String customerName, String linkMan, String linkPhone) {
        AssertUtil.isTrue(StringUtils.isBlank(customerName),"请输入客户名!");
        AssertUtil.isTrue(StringUtils.isBlank(linkMan),"请输入联系人!");
        AssertUtil.isTrue(StringUtils.isBlank(linkPhone),"请输入联系电话!");
        AssertUtil.isTrue(!(PhoneUtil.isMobile(linkPhone)),"手机号格式不合法!");
    }
}




